/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 EndpointLocation.cpp. Created on: 09.09.2009 16:31:10
 */

#include "smartobjects/network/EndpointLocation.hpp"

namespace smartobjects {

EndpointLocation::EndpointLocation(const std::string& sCoordinatorName) : m_sCoordinator(sCoordinatorName) {
}

EndpointLocation::EndpointLocation(const std::string& sCoordinatorName, URI* poURI) :
		m_sCoordinator(sCoordinatorName) {
	m_aLocations.push_back(poURI);
	poURI->addRef();
}

EndpointLocation::EndpointLocation(ISmartBus* poBus) {
	m_sCoordinator = poBus->getCoordinatorFactory()->getDefaultCoordinatorName();
}

EndpointLocation::EndpointLocation(ISmartBus* poBus, URI* poURI) {
	m_sCoordinator = poBus->getCoordinatorFactory()->getDefaultCoordinatorName();
	m_aLocations.push_back(poURI);
	poURI->addRef();
}

EndpointLocation::EndpointLocation(ISmartBus* poBus, const std::string& sReference) {

	std::string sRef = sReference;
	std::string sCoordName;

	boost::algorithm::trim(sRef);
	size_t nFindCrd = sRef.find ("{");

	if(nFindCrd!=std::string::npos) {
		sCoordName = sRef.substr(0, nFindCrd);
		boost::algorithm::trim(sCoordName);
		sRef = sRef.substr(nFindCrd + 1, sRef.length() - nFindCrd - 2);
	}

	boost::char_separator<char> oSep(";");
	typedef boost::tokenizer< boost::char_separator<char> > LocationsTokenizer;
	LocationsTokenizer oTokenizer(sRef, oSep);
	foreach (std::string sToken, oTokenizer) {
		boost::algorithm::trim(sToken);
		URI* poURI = new URI(sToken);
		m_aLocations.push_back(poURI);
	}

	if(!sCoordName.empty()) {
		m_sCoordinator = sCoordName;
	}
	else {
		m_sCoordinator = poBus->getCoordinatorFactory()->getDefaultCoordinatorName();
	}
}


EndpointLocation::~EndpointLocation() {

	foreach(URI* poURI, m_aLocations) {
		poURI->release();
	}
	m_aLocations.clear();
}

IEndpointLocation::Locations& EndpointLocation::getLocations() {
	return m_aLocations;
}

void EndpointLocation::setLocations(const Locations& aLocations) {
	m_aLocations = aLocations;
	foreach(URI* poURI, m_aLocations) {
		poURI->addRef();
	}
}

void EndpointLocation::addLocation(URI* poURI) {
	poURI->addRef();
	m_aLocations.push_back(poURI);
}

void EndpointLocation::setCoordinator(const std::string& sCoordinator) {
	m_sCoordinator = sCoordinator;
}

const std::string& EndpointLocation::getCoordinator() const {
	return m_sCoordinator;
}

void EndpointLocation::toStringReference(std::string& sResult) const {
	sResult.clear();
	typedef std::set< std::string > SortedLocations;
	SortedLocations aSortedLocations;
	foreach ( URI* poURI, m_aLocations ) {
		std::string sLocation = poURI->getScheme() + "://" +poURI->getHost()+":"+
			boost::lexical_cast<std::string>(poURI->getPort());
		aSortedLocations.insert( sLocation );
	}

	for(SortedLocations::iterator it = aSortedLocations.begin(); it!= aSortedLocations.end();) {
		sResult+= *it;
		if(++it!=aSortedLocations.end()) {
			sResult+= ";";
		}
	}

	if(m_sCoordinator.empty())
		sResult= m_sCoordinator + "{" + sResult + "}";
}


}
